package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.PgMountains;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyMountainsPersonQuery;
import com.zl.tby.model.TbyMountainsPerson;
import com.zl.tby.repository.TbyMountainsPersonRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import com.zl.tby.service.ITbyMountainsPersonService;
import com.zl.tby.vo.TbyMountainsPersonForm;
import com.zl.tby.vo.exp.TbyMountainsPersonExportExcel;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 护林员信息表 接口实现
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyMountainsPersonServiceImpl implements ITbyMountainsPersonService {

    @Db
    private TbyMountainsPersonRepo tbyMountainsPersonRepo;
    @Inject
    private ITbyFileBusinessService tbyFileBusinessService;

    private final static String db_sql = "select i_userid AS \"id\", c_userealname AS \"name\", c_usertel AS \"phone\", c_sfsh AS \"isAudit\", hly_lx AS \"userType\", pyzt AS \"status\" from xh_ht.fs_yw_base_user where i_orgid in (734, 9143)";

    @Override
    @Tran
    public R<TbyMountainsPerson> add(TbyMountainsPerson entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyMountainsPersonRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyMountainsPerson> update(TbyMountainsPerson entity) {
        TbyMountainsPerson person = tbyMountainsPersonRepo.selectById(entity.getId());
        if (person == null || person.getId() == null) {
            entity.setCreateBy(ToolsUtil.getLoginUserId());
            entity.setCreateTime(LocalDateTime.now());
            long id = tbyMountainsPersonRepo.insert(entity, true);
            entity.setId(id);
            return R.ok(entity);
        }
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyMountainsPersonRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public TbyMountainsPersonForm get(Long id) {
        TbyMountainsPerson person = tbyMountainsPersonRepo.selectById(id);
        PgMountains<TbyMountainsPersonForm> db = new PgMountains<>();
        List<Object> params = new ArrayList<>();
        params.add(id);
        List<TbyMountainsPersonForm> dbList = db.query(db_sql + " and i_userid = ?", params, TbyMountainsPersonForm.class);
        if (dbList == null || dbList.size() == 0) {
            return null;
        }
        TbyMountainsPersonForm form = dbList.get(0);
        form.merge(person);
        return form;
    }

    @Override
    public TbyPageResult<TbyMountainsPersonForm> page(TbyMountainsPersonQuery query) {
        IPage<TbyMountainsPerson> iPage = tbyMountainsPersonRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue();
        });
        List<TbyMountainsPerson> dbList = iPage.getList();
        Map<Long, TbyMountainsPerson> dbMap = new HashMap<>(0);
        for (TbyMountainsPerson entity : dbList) {
            dbMap.put(entity.getId(), entity);
        }
        PgMountains<TbyMountainsPersonForm> db = new PgMountains<>();
        List<Object> params = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        if (StringUtils.isNotEmpty(query.getName())) {
            builder.append(" and c_userealname like ?");
            params.add("%" + query.getName() + "%");
        }
        if (StringUtils.isNotEmpty(query.getIsAudit())) {
            builder.append(" and c_sfsh = ?");
            params.add(query.getIsAudit());
        }
        if (StringUtils.isNotEmpty(query.getUserType())) {
            builder.append(" and hly_lx = ?");
            params.add(query.getUserType());
        }
        if (StringUtils.isNotEmpty(query.getStatus())) {
            builder.append(" and pyzt = ?");
            params.add(query.getStatus());
        }
        List<TbyMountainsPersonForm> resultList = db.query(db_sql + builder.toString(), params, TbyMountainsPersonForm.class);
        for (TbyMountainsPersonForm form : resultList) {
            form.merge(dbMap.get(form.getId()));
        }
        return new TbyPageResult(resultList, new BigDecimal(resultList.size() + "").longValue(), query.getCurrent(), query.getSize());
    }

    @Override
    public List<TbyMountainsPersonExportExcel> excel(TbyMountainsPersonQuery query) {
        List<TbyMountainsPerson> dbList = tbyMountainsPersonRepo.selectList(mapperWhereQ -> { mapperWhereQ.whereTrue(); });
        Map<Long, TbyMountainsPerson> dbMap = new HashMap<>(0);
        for (TbyMountainsPerson entity : dbList) {
            dbMap.put(entity.getId(), entity);
        }
        PgMountains<TbyMountainsPersonForm> db = new PgMountains<>();
        List<Object> params = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        if (StringUtils.isNotEmpty(query.getName())) {
            builder.append(" and c_userealname like ?");
            params.add("%" + query.getName() + "%");
        }
        if (StringUtils.isNotEmpty(query.getIsAudit())) {
            builder.append(" and c_sfsh = ?");
            params.add(query.getIsAudit());
        }
        if (StringUtils.isNotEmpty(query.getUserType())) {
            builder.append(" and hly_lx = ?");
            params.add(query.getUserType());
        }
        if (StringUtils.isNotEmpty(query.getStatus())) {
            builder.append(" and pyzt = ?");
            params.add(query.getStatus());
        }
        Map<String, String> userTypeEnum = new HashMap<>(5);
        userTypeEnum.put("1", "专业护林员");
        userTypeEnum.put("2", "防火护林员");
        userTypeEnum.put("3", "生态护林员");
        userTypeEnum.put("4", "天然林护林员");
        userTypeEnum.put("5", "病虫护林员");
        Map<String, String> statusEnum = new HashMap<>(5);
        statusEnum.put("10", "在聘（初聘）");
        statusEnum.put("20", "解聘");
        statusEnum.put("30", "在聘（续聘）");
        List<TbyMountainsPersonForm> resultList = db.query(db_sql + builder.toString(), params, TbyMountainsPersonForm.class);
        List<TbyMountainsPersonExportExcel> dataList = new ArrayList<>();
        for (TbyMountainsPersonForm form : resultList) {
            form.merge(dbMap.get(form.getId()));
            dataList.add(new TbyMountainsPersonExportExcel(form, userTypeEnum, statusEnum));
        }
        return dataList;
    }
}
